package com.example.petshopping.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.model.Service;

import java.util.List;

/**
 * 上门服务列表适配器
 */
public class ServiceAdapter extends RecyclerView.Adapter<ServiceAdapter.ServiceViewHolder> {

    private Context context;
    private List<Service> serviceList;
    private OnItemClickListener listener;

    public ServiceAdapter(Context context, List<Service> serviceList) {
        this.context = context;
        this.serviceList = serviceList;
    }

    @NonNull
    @Override
    public ServiceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_service_card, parent, false);
        return new ServiceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ServiceViewHolder holder, int position) {
        Service service = serviceList.get(position);
        
        // 设置商店信息
        holder.tvShopName.setText(service.getShopName());
        holder.tvShopRating.setText(service.getRating());
        holder.tvShopFans.setText(service.getFans());
        
        // 加载头像
        // 这里应该使用图片加载库如Glide或Picasso
        // 示例：Glide.with(context).load(service.getAvatarUrl()).into(holder.imgAvatar);
        // 为简化，这里使用了默认图片
        
        // 设置地址和距离信息
        holder.tvAddress.setText(service.getAddress());
        holder.tvDistance.setText(service.getDistance());
        
        // 清除之前的服务项目
        holder.containerServiceItems.removeAllViews();
        
        // 添加服务项目
        for (Service.ServiceItem item : service.getServiceItems()) {
            View serviceItemView = LayoutInflater.from(context).inflate(
                    R.layout.item_service_detail, holder.containerServiceItems, false);
            
            TextView tvPrice = serviceItemView.findViewById(R.id.tv_service_price);
            TextView tvDesc = serviceItemView.findViewById(R.id.tv_service_desc);
            
            tvPrice.setText(item.getPrice());
            tvDesc.setText(item.getDescription());
            
            holder.containerServiceItems.addView(serviceItemView);
        }
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(service, position);
            }
        });
        
        holder.btnEnter.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEnterClick(service, position);
            }
        });
        
        holder.tvAddress.setOnClickListener(v -> {
            if (listener != null) {
                listener.onAddressClick(service, position);
            }
        });
        
        holder.imgRightArrow.setOnClickListener(v -> {
            if (listener != null) {
                listener.onAddressClick(service, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return serviceList != null ? serviceList.size() : 0;
    }

    public static class ServiceViewHolder extends RecyclerView.ViewHolder {
        ImageView imgAvatar, imgRightArrow;
        TextView tvShopName, tvShopRating, tvShopFans, tvAddress, tvDistance;
        Button btnEnter;
        LinearLayout containerServiceItems;

        public ServiceViewHolder(@NonNull View itemView) {
            super(itemView);
            imgAvatar = itemView.findViewById(R.id.img_avatar);
            tvShopName = itemView.findViewById(R.id.tv_shop_name);
            tvShopRating = itemView.findViewById(R.id.tv_shop_rating);
            tvShopFans = itemView.findViewById(R.id.tv_shop_fans);
            btnEnter = itemView.findViewById(R.id.btn_enter);
            tvAddress = itemView.findViewById(R.id.tv_address);
            imgRightArrow = itemView.findViewById(R.id.img_right_arrow);
            tvDistance = itemView.findViewById(R.id.tv_distance);
            containerServiceItems = itemView.findViewById(R.id.container_service_items);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Service service, int position);
        void onEnterClick(Service service, int position);
        void onAddressClick(Service service, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
} 